package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CareerListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditChooseBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.gongneng.renshi.zuzhiguanli.CareerDescActivity;
import com.eqdd.yiqidian.ui.select.SelectPostActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

public class ApplyRenliActivity extends SlimBaseMultiEditActivity {
    private RecyclerViewCustom dataBinding;

    private String msg;
    @ToSave
    CareerListResultBean careerBean;
    private Bundle bundle;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "人力需求申请");
        initTopRightText("提交", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.APPLY_RENLI)
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("depid", careerBean.getDepartid())
                        .params("postid", careerBean.getId())
                        .params("XYrenshu", setParam(3))
                        .params("DLZrenshu", setParam(4))
                        .params("recruitRenShu", setParam(5))
                        .params("recruitReason", setParam(6))
                        .params("demandAtWorkTime", setParam(8))
                        .params("remark", setParam(9))
                        .params("recruitType", setParam(10))
                        .params("comid", careerBean.getCompanyid())
                        .params("createrName", AppConstent.user.getUsername())
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParams() {
        for (int i = 0; i < slimAdapterEx.getData().size(); i++) {
            if (slimAdapterEx.getDataItem(i).isNeed()) {
                if (isParamBlank(i)) {
                    msg = "请输入" + slimAdapterEx.getDataItem(i).getTitle().replace("*", "");
                    return false;
                }
            }
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 1:
                bundle = new Bundle();
                bundle.putInt(Config.COM_ID,AppConstent.user.getCompanyId());
                bundle.putInt(Config.DEPARTMENT_ID,AppConstent.user.getDepartId());
                startActivityForResult(SelectPostActivity.class, bundle, Config.SELECT_POST);
                break;
            case 7:
                if (careerBean == null) {
                    ToastUtil.showShort("请先选择职位");
                } else {
                    bundle = new Bundle();
                    bundle.putBoolean(Config.IS_UPDATE,false);
                    bundle.putInt(Config.ID, careerBean.getId());
                    bundle.putString("postName", careerBean.getName());
                    bundle.putString("depName", AppConstent.user.getDepartment());
                    startActivity(CareerDescActivity.class, bundle);
                }
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditApplyTitleBean());
        datas.add(new SlimEditEnterBean("*申请职位", "", 1, true, "请选择").setNeed(true));
        datas.add(new SlimEditHintBean("*职位编制人数", "职位编制人数", 2).setNeed(true));
        datas.add(new SlimEditHintBean("*职位现有人数", "职位现有人数", 3).setNeed(true));
        datas.add(new SlimEditHintBean("*待离职人数", "待离职人数", 4).setNeed(true));
        datas.add(new SlimEditEnterBean("*申请人数", "", 5, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditEnterBean("*招聘原因", "", 6, "请输入").setNeed(true));
        datas.add(new SlimEditHintBean("工作职责", "查看", 7).setDeal(true));
        datas.add(new SlimEditTimeBean("*要求到岗时间", "", 8, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
        datas.add(new SlimEditEnterBean("备注", "", 9, "请输入"));
        datas.add(new SlimEditChooseBean("*招聘渠道", "", 10, "请选择", new String[]{"内招", "外招", "內外招"}).setNeed(true));
    }

    protected void getNums() {

        OkGo.<CareerNum>post(HttpConfig.BASE_URL + HttpConfig.GET_DEPART_NUMS)
                .params("userGuid", user.getGuid())
                .params("comid", careerBean.getCompanyid())
                .params("postid", careerBean.getId())
                .execute(new DialogCallBack<CareerNum>(ApplyRenliActivity.this) {
                    @Override
                    public void onSuccess(Response<CareerNum> response) {
                        CareerNum httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getData().get(2).setContent(httpResult.getBZrenshu() + "");
                            slimAdapterEx.getData().get(3).setContent(httpResult.getXYrenshu() + "");
                            slimAdapterEx.getData().get(4).setContent(httpResult.getDLZrenshu() + "");
                            slimAdapterEx.notifyItemRangeChanged(2, 3);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.SELECT_POST && resultCode == Config.SUCCESS) {
            careerBean = (CareerListResultBean) data.getSerializableExtra(Config.BEAN);
            slimAdapterEx.getDataItem(1).setContent(careerBean.getName());
            slimAdapterEx.notifyItemChanged(1);
            getNums();
        }
    }

    class CareerNum {

        /**
         * status : 200
         * msg : 查询成功！
         * BZrenshu : 0
         * XYrenshu : 0
         * DLZrenshu : 1
         */

        private int status;
        private String msg;
        private int BZrenshu;
        private int XYrenshu;
        private int DLZrenshu;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public int getBZrenshu() {
            return BZrenshu;
        }

        public void setBZrenshu(int BZrenshu) {
            this.BZrenshu = BZrenshu;
        }

        public int getXYrenshu() {
            return XYrenshu;
        }

        public void setXYrenshu(int XYrenshu) {
            this.XYrenshu = XYrenshu;
        }

        public int getDLZrenshu() {
            return DLZrenshu;
        }

        public void setDLZrenshu(int DLZrenshu) {
            this.DLZrenshu = DLZrenshu;
        }
    }
}
